package br.net.atasacramental.trimestre.business;
import br.net.atasacramental.atividade.business.EventoBLL;
import br.net.atasacramental.atividade.model.Evento;
import br.net.atasacramental.atividade.model.EventoData;
import br.net.atasacramental.trimestre.model.Trimestre;
import br.net.atasacramental.trimestre.dao.TrimestreDAO;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import under.wsl.service.Service;
/**
 * @author alexandre.araujo
 */
public class TrimestreBLL{
    private TrimestreDAO dao = null;
    public TrimestreBLL() {
        this.dao = new TrimestreDAO();
    }
    @Service(cache=true)
    public List<Trimestre> get(){
        return this.dao.get();
    }
    @Service()
    public Trimestre getByIdTrimestre(int idTrimestre) {
        return this.dao.getByIdTrimestre(idTrimestre);
    }
    @Service()
    public List<Trimestre> getByAno(int ano) {
        return this.dao.getByAno(ano);
    }
    @Service()
    public List<Trimestre> getByNrTrimestre(int nrTrimestre) {
        return this.dao.getByNrTrimestre(nrTrimestre);
    }
    @Service(cache=true)
    public List<Trimestre> getDisponiveis() {
    	return this.getBySnAberto("S");
    }
    @Service()
    public List<Trimestre> getBySnAberto(String snAberto) {
    	List<Trimestre> lstrim = this.dao.getBySnAberto(snAberto); 
    	int tmLtrim = lstrim.size();
    	if(tmLtrim>0){	    	
    		String[] diasemana = {"dom","seg","ter","qua","qui","sex","sab"};
    		EventoBLL evtbll = new EventoBLL();
	    	DateFormat df = new SimpleDateFormat ("dd-MM-yyyy");	    	
	    	Calendar dtI = Calendar.getInstance(); 
	    	Calendar dtF = Calendar.getInstance(); 	    	
	    	for(int x = 0;x<tmLtrim;x++){	    	
	    		
	    		int intervalo = 0;	    		
	    		dtF.set(Calendar.YEAR,lstrim.get(x).getAno());
	    		dtF.set(Calendar.MONTH,0);
	    		dtF.set(Calendar.DAY_OF_MONTH,1);	    		
	    		dtF.add(Calendar.MONTH,(lstrim.get(x).getNrTrimestre()*3)-1);
	    		dtF.add(Calendar.MONTH,1);
	    		dtF.add(Calendar.DAY_OF_MONTH,-1);
	    	
	    		dtI = (Calendar) dtF.clone();
		    	dtI.add(Calendar.MONTH,-2);	
		    	dtI.set(Calendar.DAY_OF_MONTH,1);
		    	
		    	intervalo = (dtF.get(Calendar.DAY_OF_YEAR) - dtI.get(Calendar.DAY_OF_YEAR)) + 1;
		    	
		    	//System.out.println("TRIM3:"+lstrim.get(x).getNrTrimestre()+"  DI:"+df.format (dtI.getTime())+"  DF:"+df.format (dtF.getTime())+" int:"+intervalo);
		    	
		    	List<EventoData> evtsdata = new ArrayList<EventoData>();
		    	//df.format (dtI.getTime());df.format (dtF.getTime());
		    	
		    	List<Evento> evts = evtbll.getByIdTrimestre(lstrim.get(x).getIdTrimestre());
		    	//margem de seguranca para inicio de trimestre
		    	dtI.add(Calendar.DATE,17);
		    	intervalo-=17;
		        for (int i = 0;i<intervalo;i++) {		        	  
		            //System.out.println (df.format (dtI.getTime()));	            
		            Boolean ok = true;
		            for(int x2=0; x2 <evts.size() ; x2++){
		            	dtF.setTime(evts.get(x2).getMomento());
		            	if(dtF.get(Calendar.DAY_OF_YEAR) == dtI.get(Calendar.DAY_OF_YEAR)){
		            		evts.remove(x2);
		            		ok = false;
		            		break;
		            	}
		            }
		            if(ok){
		            	evtsdata.add(new EventoData(x+1, df.format(dtI.getTime())+" "+diasemana[dtI.get(Calendar.DAY_OF_WEEK)-1]+".", dtI.getTime()));			            
		            }		            
		            dtI.add(Calendar.DATE,1);
		            
		        }  
			     
			     lstrim.get(x).setDtDisponivel(evtsdata);
			     
		    	}
    	}
    	
        return lstrim;
    }
    @Service(remove={"TrimestreBLL.get","TrimestreBLL.getDisponiveis"})   
    public int insert(Trimestre t){    
        if(this.dao.insert(t)){            
            return t.getIdTrimestre();
        }
        return 0;
    }
    @Service(remove={"TrimestreBLL.get","TrimestreBLL.getDisponiveis"})
    public boolean update(Trimestre t){
        return this.dao.update(t);
    }
    @Service(remove={"TrimestreBLL.get","TrimestreBLL.getDisponiveis"})
    public boolean delete(Trimestre t){
         return this.dao.delete(t);
    }  
}